<template>
	<view class="invoice">
		<navBar />
		<!-- 发票筛选 -->
		<view class="invoices">
			<view class="titles">发票中心</view>
			<!-- 搜索 -->
			<view class="invoice-search">
				<input v-model="billingCode" placeholder="请输入开票编号" class="invoice-input"
					@confirm="onSearchInvoiceConfirm" />
				<uni-icons type="search" size="20" color="#bbb" class="invoice-icon"></uni-icons>
				<view class="invoice-btn" @click="onChangeInvoice()">搜索</view>
			</view>
		</view>
		<!-- 列表 -->
		<view class="dtc">
			<view class="dtc-item" v-for="(item,index) in invoiceList" :key="index">
				<view class="invo">开票编号：<text class="code">{{ item.billingCode }}</text></view>
				<view class="dtc-c invo">开票项目：<text class="status">{{ item.billingItem }}</text></view>
				<view class="dtc-c invo">Decimal(20,2)含税金额：<text class="colorr">{{ item.taxAmount }}</text></view>
				<!-- <view class="dtc-c">2024/12/3 16:57:44</view> -->
				<view class="dtc-t invo">{{ $getRelativeTime(item.createDate) }}</view>
				<uni-collapse ref="collapse">
					<uni-collapse-item title="附件：">
						<view class="attachsc">
							<view class="attachs" v-for="(atts,indexs) in item.attach" :key="indexs">
								<text class="attname">{{ atts.attName }}</text>
								<view class="controls">
									<text class="rolsl" @click="preview(atts.attURL)">预览</text>
									<text @click="download(atts.attURL)">下载</text>
								</view>
							</view>
						</view>
					</uni-collapse-item>
				</uni-collapse>
			</view>
			
		</view>
		<view style="height: 10rpx;"></view>
		<tabBar selectedIndex="1" selectedTab="1" />
	</view>
</template>

<script>
	// 引入api
	import {
		getProgramBillingInfo, // 发票查询
	} from "@/proxy/program/program.api.js";
	import {
		FileHandler
	} from "@/utils/fileHandler";
	export default {
		data() {
			return {
				invoiceList: [],
				billingCode: "", //开票编号
				ImageGList: ["jpeg", "jpg", "png"]
			}
		},
		async onLoad() {
			await this.getProgramBillingInfoData()
		},
		methods: {
			async onSearchInvoiceConfirm() {
				await this.getProgramBillingInfoData()
			},
			async onChangeInvoice() {
				await this.getProgramBillingInfoData()
			},
			// 获取发票列表
			async getProgramBillingInfoData() {
				// 发送请求
				const {
					customerCode
				} = uni.getStorageSync("userInfo");
				if (!customerCode) {
					return this.$toast({
						title: "请前往小程序授权登录",
						// icon: "error",
					});
				}
				// 发送请求
				try {
					const result = await getProgramBillingInfo([{
						billingCode: this.billingCode,
						customerCode: customerCode,
					}]);
					if (result.code == 200) {
						this.invoiceList = result.data;
						console.log("查询发票成功！！！", result);
					} else {
						return this.$toast({
							title: result.data.msg,
						});
					}
				} catch (error) {
					// console.error("请求失败", error);
					return this.$toast({
						title: "请求失败",
						icon: "error",
					});
				}
			},
			preview(filePath){
				FileHandler.preview(filePath);  // 调用预览方法
			},
			download(filePath){
				FileHandler.download(filePath);  // 调用下载方法
			}
		}
	}
</script>

<style lang="scss">
	// page {
	// 	background-color: rgb(249, 250, 251);
	// }
	/* app端 */
	/* #ifdef APP-PLUS || H5 */
	#app,
	/* #endif */
	page {
		background-color: #F9FAFB;
		height: 100%;
	}

	.invoice {
		display: flex;
		flex-direction: column;
		justify-content: center;
		height: 100vh;

		.invoices {
			display: flex;
			align-items: center;
			margin: 10rpx 20rpx;
			justify-content: space-between;

			.titles {
				// margin: 20rpx 20rpx 0;
				font-size: 30rpx;
				width: 200rpx;
				text-align: center;
				/* background-image: linear-gradient(to right, rgb(224,247,237), rgb(235,243,254)); */
				/* color: #fff; */
				border: 1rpx solid #ddd;
				border-radius: 30rpx;
				padding: 5rpx 0rpx;
			}

			.invoice-search {
				display: flex;
				align-content: center;
				// padding: 0rpx 20rpx;
				position: relative;
				background: #FFF;
				border: 1rpx solid #f1f1f1;
				flex: 1;
				margin-left: 10rpx;
				border-radius: 10rpx;
				overflow: hidden;

				.invoice-input {
					flex: 1;
					height: 70rpx;
					line-height: 70rpx;
					// border-radius: 40rpx;
					padding-left: 60rpx;
					font-size: 27rpx;
				}

				.invoice-icon {
					position: absolute;
					left: 15rpx;
					top: 52%;
					-webkit-transform: translateY(-50%);
					transform: translateY(-50%);
				}

				.invoice-btn {
					width: 90rpx;
					text-align: center;
					font-size: 28rpx;
					height: 70rpx;
					line-height: 70rpx;
					color: #fff;
					background: #2698fb;
				}
			}
		}

		.dtc {
			flex: 1;
			overflow-y: auto;

			// margin: 0rpx 20rpx;
			.dtc-item {
				background-color: #fff;
				padding: 0 20rpx 20rpx;
				border-radius: 20rpx;
				font-size: 25rpx;
				margin: 20rpx;
				box-shadow: #bbb 0 0 8rpx;

				// border: 1rpx solid #ddd;
				// box-shadow: 0 0 30rpx rgba(0, 0, 0, 0.1);
				.dtc-c {
					font-size: 24rpx;
					color: #6C7381;
					// margin: 10rpx 0;
				}

				.code {
					font-size: 30rpx;
					font-weight: bold;
				}

				.dtc-t {
					font-size: 22rpx;
					color: #6C7381;
					text-align: right;
					// margin: 10rpx 0;
				}

				.invo {
					// line-height: 60rpx;
					padding: 10rpx 0;
				}

				// 列表
				.attachsc {
					.attachs {
						display: flex;
						justify-content: space-between;
						padding: 10rpx 0;

						.attname {}

						.controls {
							color: #0081ff;

							.rolsl {
								margin-right: 10rpx;
							}
						}
					}
				}
			}
		}
	}

	// .colors{
	// 	color: #6C7381;
	// }
	// .colorb{
	// 	color: #000;
	// 	font-size: 25rpx;
	// 	font-weight: bold;
	// }
	.colorr {
		color: #FF0000;
		font-size: 28rpx;
		font-weight: bold;
	}

	.status {
		color: #FFF;
		background: #FF0000;
		padding: 5rpx 10rpx;
		border-radius: 10rpx;
	}

	.uni-collapse-item__title-wrap>.uni-collapse-item__title-box {
		height: unset !important;
		min-height: 60rpx !important;
		line-height: 60rpx !important;
		padding: 0rpx !important;
	}

	.uni-collapse-item__title-text {
		font-size: 25rpx !important;
	}
</style>